package com.tpo.jnotes.client.lib.panel;

import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class MemoryPanel extends JPanel implements Runnable
{
    JProgressBar progressBar = new JProgressBar();

    public MemoryPanel()
    {
        setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) );

        JLabel lblMemory = new JLabel( "Memory" );
        add( lblMemory );
        progressBar.setString( "0 kb" );
        progressBar.setBackground( Color.WHITE );
        progressBar.setForeground( new Color( 51, 204, 204 ) );
        progressBar.setStringPainted( true );

        add( progressBar );

        new Thread( this ).start();
    }

    @Override
    public void run()
    {
        Runtime runtime = Runtime.getRuntime();
        long totalMemory;
        long usedMemory;

        while( true )
        {
            try
            {
                totalMemory = runtime.totalMemory() / 1024 / 1024;
                usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
                progressBar.setMaximum( (int)totalMemory );
                progressBar.setValue( (int)usedMemory );
                progressBar.setString( progressBar.getValue() + " MB" );
                Thread.sleep( 5000 );
            }
            catch( InterruptedException e )
            {
                e.printStackTrace();
            }

        }

    }

}
